package org.itboys.core.service;

import com.aliyuncs.exceptions.ClientException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.order.SundaySendOrderMapper;
import org.itboys.core.dao.order.SundaySendOrderModelMapper;
import org.itboys.core.entity.mysql.order.SundaySendOrder;
import org.itboys.core.entity.mysql.order.SundaySendOrderModel;
import org.itboys.core.util.SMS.AliyunSMSUtilWarn;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作者：zzq
 * 时间：2018/12/25
 * 描述：
 */
public class SundayJobService {
    @Autowired
    private SundaySendOrderMapper sendOrderMapper;
    @Autowired
    private SundaySendOrderModelMapper sendOrderModelMapper;


    public void countOrder() throws ClientException {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("status", SundayCommonConstants.send_order_status_ysp);
        List<SundaySendOrder> sendOrders = sendOrderMapper.select(param);
        if (ListUtil.isNull(sendOrders)) {
            return;
        }
        Date now = new Date();
        List<Long> orderIds = sendOrders.stream().map(e -> e.getId()).collect(Collectors.toList());
        param.clear();
        param.put("orderIds", orderIds);
        List<SundaySendOrderModel> sendOrderModels = sendOrderModelMapper.select(param);
        for (SundaySendOrder sendOrder : sendOrders) {
            //实际使用金额
            BigDecimal realAmount = BigDecimal.ZERO;
            //充值的金额
            BigDecimal rentAmount = sendOrder.getRentAmount();
            //订单一天的金额
            BigDecimal oneAmount=BigDecimal.ZERO;
            Date receiveDate = sendOrder.getReceiveTime();
            long diff = now.getTime() - receiveDate.getTime();
            long days = diff / (1000 * 60 * 60 * 24);
            days = days <= 0 ? 1 : days;
            for (SundaySendOrderModel orderModel :sendOrderModels) {
                if(orderModel.getOrderId().longValue()!=sendOrder.getId().longValue()){
                    continue;
                }
                oneAmount = oneAmount.add(orderModel.getRentPrice());
                realAmount = realAmount.add(orderModel.getRentPrice().multiply(new BigDecimal(orderModel.getNum())).multiply(new BigDecimal(days)));
            }
            if(realAmount.compareTo(rentAmount)==1){
                //如果实际费用大于租赁费用则设置为已到期
                sendOrder.setStatus(SundayCommonConstants.send_order_status_ydq);
                sendOrderMapper.update(sendOrder);
            }
            if(realAmount.add(oneAmount.multiply(new BigDecimal("3"))).compareTo(rentAmount)==1){
                // 如果充值的金额小于实际使用金额  加上订单三天的消耗金额   短信通知用户
                AliyunSMSUtilWarn.sendSms(sendOrder.getMobile(), "");
            }
        }


    }
}
